package com.controller;

import com.pojo.Order;
import com.pojo.Product;
import com.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Controller
public class UserController {
    @RequestMapping("/getUserId")
    public void getUserID(HttpServletRequest request) {
        //HttpServletRequest是Servlet的请求对象
        String userID = request.getParameter("userid");
        System.out.println( "userid="+userID );
    }
    @RequestMapping("/gerUserMapping")
    public void gerUserAndId(String username,Integer id) {
        System.out.println("username = " + username);
        System.out.println("id = " + id);
    }
    @RequestMapping("/getUserName")
    public void getUserName(
            @RequestParam (value = "name", required = false,
            defaultValue = "itheima") String username) {
        System.out.println("username = " + username);
    }

    // pojo绑定
    @RequestMapping("/registerUser")
    public void registerUser(User user) {
        String name = user.getName();
        String password= user.getPassword();
        System.out.println("name = " + name);
        System.out.println("password = " + password);

    }
    @RequestMapping("/findOrderWithUser")
    public void fin(User user) {
        String username = user.getName();
        String orderId= user.getOrder().getOrderId();
        System.out.println("username = " + username);
        System.out.println("orderId = " + orderId);
    }
    @RequestMapping("/showOrderrs")
    public void showOrderrs(User user) {
        List<Order> orders = user.getOrders();
        List<String> addressList = user.getAddress();
        System.out.println("订单");
        Order order;
        for (int i = 0; i < orders.size(); i++) {
            System.out.println(orders.get(i).getOrderId());
            order = orders.get(i);
            String address = addressList.get(i);
            System.out.println("订单Id" + order.getOrderId());
            System.out.println("订单配置地址：" + address);
        }

    }
    @RequestMapping("/orderInfo")
    public void getOrderInfo(Order order) {
        String orderId=order.getOrderId();
        HashMap<String, Product> orderInfo=order.getProductInfo();
        Set<String>keys=orderInfo.keySet();
        System.out.println("订单id："+orderId);
        System.out.println("订单商品信息：");
        for (String key:keys) {
            Product product=orderInfo.get(key);
            String proId=product.getProId();
            String proName=product.getProName();
            System.out.println(key+"类~"+"商品id："+proId+"，商品名称："+proName);
        }
    }

    @RequestMapping("/user")
    public String user(Model model) {
        User user = new User();
        user.setName("张三");
        user.setPassword("123456");
        model.addAttribute("user", user);
        return "user"; //  /WEB-INF/pages/user.jsp
    }
    @RequestMapping("/ouser")
    public ModelAndView o() {
        User user = new User();
        user.setName("张四");
        user.setPassword("123456");
        ModelAndView mv=new ModelAndView();
        mv.addObject("user", user);
        mv.setViewName("user");
        return mv;
    }
}
